import redis

class RedisMethod:
    #初始化连接
    #默认使用0号数据库
    def __init__(self,db=0):
        self.connect = redis.StrictRedis(db=db,decode_responses=True)
    #插入
    def push(self,key,*values,flag=True):
        if flag:
            self.connect.rpush(key,*values)
        else:
            self.connect.lpush(key,*values)
    #查看value
    def watch(self,key,start,end):
        print(self.connect.lrange(key,start,end))
    #查看key
    def watch_key(self):
        print(self.connect.keys('*'))
    #修改
    def lset(self,key,index,value):
        self.connect.lset(key,index,value)
    #删除value(左删、右删)
    def pop(self,key,flag=True):
        if flag:
            #默认右删
            self.connect.rpop(key)
        else:
            self.connect.lpop(key)
    #删除value(指定value)
    def pop_by_value(self,key,count,value):
        self.connect.lrem(key,count,value)
    #删除key
    def delete(self,*keys):
        self.connect.delete(*keys)

if __name__ == '__main__':
    rm = RedisMethod()
    print('----------创建列表，插入一些值')
    rm.push('fruit','apple','pairs','watermelon','apple','apple','apple','apple')
    rm.push('sport','football','basketball','baseball')
    print('----------查看列表名称')
    rm.watch_key()
    print('----------查看列表内容')
    rm.watch('fruit',0,5)
    rm.watch('sport',0,5)
    print('----------修改列表值')
    rm.lset('fruit',0,'orange')
    rm.watch('fruit',0,5)
    print('----------删除列表值')
    rm.pop('sport',flag=False)
    rm.pop_by_value('fruit',0,'apple')
    rm.watch('fruit',0,5)
    rm.watch('sport',0,5)
    print('----------删除整个列表')
    rm.delete('fruit','sport')
    rm.watch_key()